/*
# Miahx© 框架消息 FastDDS API 的 KANZI 支持类（C++端）测试文件

Version: 0.4.10,
Last updated: October 15, 2024

## Contributor(s)
* YAO Naiming <[naiming@iscas.ac.cn](mailto:naiming@iscas.ac.cn)>

## Change log
### v0.4.10
- .15: [新增] 迁移实现

---
Copyright © 2024-2025 MIAHX Contributors, ISCAS, and ASIC. All Rights Reserved.
Licensed under Apache-2.0.
*/

#include <miahx_cpp_wrapper>
#include <fstream>


class UpdateScript {
public:
    void init() {
        mWrapper = MiahxWrapper::get_instance();
    }

    void update() {
        if (nullptr != mWrapper) {
            try {
                auto &vcls = mWrapper->get_stream_handler("vcls");
                auto frame = vcls.next();
                if (!frame.empty()) {
                    cout << frame.dump(4) << "\n" << endl;
                } else {
                    cout << "No data" << endl;
                }
//            auto frame = vcls.next_as_json_str();
//            if (!frame.empty()) {
//                cout << frame << "\n" << endl;
//            } else {
//                cout << "No data" << endl;
//            }
            } catch (const std::exception &ex) {
                cerr << ex.what() << endl;
            }
        }
    }

private:
    shared_ptr<MiahxWrapper> mWrapper;
};

class CallbackScript {
public:
    void init() {
        mMiahxWrapper = MiahxWrapper::get_instance();
        auto &vcls = mMiahxWrapper->get_stream_handler("vcls");
        mMiahxWrapper->set_stream_listener("vcls", [&](const json &frame) {
            cout << frame.dump(4) << "\n" << endl;
            ofstream stream("D:\\kanzi 24.10.21\\sample.json");
            stream << frame.dump(4);
        });
    }

private:
    shared_ptr<MiahxWrapper> mMiahxWrapper;
};

void simulate_a_coroutine(UpdateScript &script) {
    script.init();

    size_t n = 10;
    while (n > 0) {
        script.update();
        this_thread::sleep_for(chrono::seconds(1));
        n--;
    }
    cout << "Finished" << endl;
}

int main() {
//    UpdateScript script;
//    thread th(simulate_a_coroutine, ref(script));
//    th.join();

    CallbackScript script2;
    script2.init();
    getchar();

    return 0;
}
